package hoang.hut.framework.database.entity;

import hoang.hut.framework.database.DBDefinition;
import hoang.hut.framework.database.model.HistoryModel;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class HistoryEntiry extends BaseEntity implements DBDefinition{

	public HistoryEntiry(Context mContext) {
		super(TABLE_HISTORY, mContext);
	}
	
	@Override
	public long add(Object obj) {
		
		HistoryModel mHistory = (HistoryModel) obj;
		ContentValues values = new ContentValues();
		values.put(COLUMN_HISTORY_ID, mHistory.getId());
		values.put(COLUMN_HISTORY_FROM, mHistory.getFrom());
		values.put(COLUMN_HISTORY_TO, mHistory.getTo());
		values.put(COLUMN_HISTORY_CONTENT, mHistory.getContent());
		values.put(COLUMN_HISTORY_TIME, mHistory.getTime());
		return super.add(values);
	}

	@Override
	public boolean update(Object obj) {

		HistoryModel mHistory = (HistoryModel) obj;
		ContentValues values = new ContentValues();
		values.put(COLUMN_HISTORY_FROM, mHistory.getFrom());
		values.put(COLUMN_HISTORY_TO, mHistory.getTo());
		values.put(COLUMN_HISTORY_CONTENT, mHistory.getContent());
		values.put(COLUMN_HISTORY_TIME, mHistory.getTime());
		String where = COLUMN_HISTORY_ID + "=" + mHistory.getId();
		return super.update(values, where);
	}

	@Override
	public int delete(Object obj) {
		HistoryModel model = (HistoryModel) obj;
		String where = COLUMN_HISTORY_ID + "="	+ model.getId();
		return super.delete(where); 
	}

	@Override
	public Object getById(int id) {
		HistoryModel model = new HistoryModel();
		String sql = "SELECT * FROM " + TABLE_HISTORY + " WHERE "
				+ COLUMN_HISTORY_ID + " = " + id;
		Cursor mCursor = super.query(sql);
		if (mCursor != null && mCursor.moveToFirst()) {
			model.setId(mCursor.getInt(0));
			model.setFrom(mCursor.getString(1));
			model.setTo(mCursor.getString(2));
			model.setContent(mCursor.getString(3));
			model.setTime(mCursor.getString(4));
		} else {
			model = null;
		}
		mCursor.close();
		return model;
	}

	@Override
	public ArrayList<Object> getAll() {
		ArrayList<Object> listObject = new ArrayList<Object>();
		try {
			if (null != _db)
				_db = null;
			_db = _dbhandler.getWritableDatabase();
			Cursor mCursor = _db.query(DBDefinition.TABLE_HISTORY, null, null,
					null, null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
				do {
					int id = mCursor.getInt(0);
					String from = mCursor.getString(1);
					String to = mCursor.getString(2);
					String content = mCursor.getString(3);
					String time = mCursor.getString(4);
					
					HistoryModel history = new HistoryModel(id, from, to, content, time);

					listObject.add(history);
				} while (mCursor.moveToNext());
			}
			mCursor.close();
			_db.close();
		} catch (Exception e) {
			listObject.clear();
		}
		return listObject;
	}
	/**
	 * get all list history in database
	 * 
	 * @return: ArrayList<ObjectHistory>
	 */
	public ArrayList<HistoryModel> getAllList(){
		ArrayList<HistoryModel> listHistory = new ArrayList<HistoryModel>();
		
		String sql = "SELECT * FROM " + TABLE_HISTORY;
		Cursor mCursor = super.query(sql);
		if (mCursor != null) {
			mCursor.moveToFirst();
			do {
				int id = mCursor.getInt(0);
				String from = mCursor.getString(1);
				String to = mCursor.getString(2);
				String content = mCursor.getString(3);
				String time = mCursor.getString(4);
				
				HistoryModel history = new HistoryModel(id, from, to, content, time);

				listHistory.add(history);
			} while (mCursor.moveToNext());
		} 
		mCursor.close();
		
		return listHistory;
	}

}
